package com.android.datn.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.android.datn.R;
import com.android.datn.activities.AHome;
import com.android.datn.helper.DBBinding;
import com.android.datn.objects.Album;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;

public abstract class AlbumPagerAdapter extends PagerAdapter {
	LayoutInflater inflater;
	public Activity mContext;
	private ImageLoader imageLoader = ImageLoader.getInstance();
	private List<Album> mAlbumItems;
	public DisplayImageOptions options = new DisplayImageOptions.Builder()
			.showStubImage(R.drawable.background).cacheInMemory().cacheOnDisc()
			.build();
	public AHome parent;

	public AlbumPagerAdapter(Activity context, List<Album> albumItems) {
		this.parent = parent;
		mContext = context;
		mAlbumItems = albumItems;
		inflater = context.getLayoutInflater();
	}

	@Override
	public int getCount() {
		if (mAlbumItems.size() < 10)
			return 1;
		else
			return (mAlbumItems.size() - 1) / 9 + 1;
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		((ViewPager) container).removeView((View) object);
		// super.destroyItem(container, position, object);
	}

	@Override
	public void finishUpdate(View container) {
	}

	@SuppressWarnings("deprecation")
	@Override
	public Object instantiateItem(View view, final int position) {
		GridView layout = new GridView(mContext);
		layout.setNumColumns(3);
		layout.setVerticalScrollBarEnabled(false);
		layout.setLayoutParams(new ViewGroup.LayoutParams(
				ViewGroup.LayoutParams.FILL_PARENT,
				ViewGroup.LayoutParams.FILL_PARENT));
		layout.setVerticalSpacing(10);
		layout.setHorizontalSpacing(10);
		layout.setAdapter(new AlbumAdapter(mContext, position, view));
		layout.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapterView, View view,
					int pos, long id) {
				albumItemClick(position * 9 + pos);

			}
		});
		((ViewPager) view).addView(layout);
		return layout;
	}

	protected abstract void albumItemClick(int position);

	@Override
	public boolean isViewFromObject(View view, Object object) {
		return view.equals(object);
	}

	@Override
	public void restoreState(Parcelable state, ClassLoader loader) {
	}

	@Override
	public Parcelable saveState() {
		return null;
	}

	@Override
	public void startUpdate(View container) {
	}

	private class AlbumAdapter extends BaseAdapter {
		private Context mContext;
		private int mPage;
		private View mParent;

		public AlbumAdapter(Context context, int page, View parent) {
			this.mContext = context;
			this.mPage = page;
			mParent = parent;
		}

		@Override
		public int getCount() {
			return mPage * 9 + 9 > mAlbumItems.size() ? mAlbumItems.size()
					- mPage * 9 : 9;
		}

		@Override
		public Album getItem(int position) {
			if (position >= 0 && position < getCount())
				return mAlbumItems.get(mPage * 9 + position);
			else
				return null;
		}

		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {
			final ViewHolder holder;
			View item = convertView;
			Album albumItem = getItem(position);
			if (item == null) {
				LayoutInflater inflater = (LayoutInflater) mContext
						.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				item = inflater.inflate(R.layout.view_album, null);
				holder = new ViewHolder();
				item.setLayoutParams(new GridView.LayoutParams(
						GridView.LayoutParams.MATCH_PARENT,
						(mParent.getHeight() / 3)-20));
				holder.image = (ImageView) item.findViewById(R.id.imgAlbum);
				holder.progress = (ProgressBar) item
						.findViewById(R.id.progress);
				item.setTag(holder);
			} else {
				holder = (ViewHolder) item.getTag();
			}
			if (!"".equals(albumItem.getImage()))
				imageLoader.displayImage(albumItem.getImage().toString(),
						holder.image, options, new ImageLoadingListener() {
							@Override
							public void onLoadingStarted() {
								// TODO Auto-generated method stub
								holder.progress.setVisibility(View.VISIBLE);
							}

							@Override
							public void onLoadingFailed(FailReason failReason) {
								// TODO Auto-generated method stub
								holder.progress.setVisibility(View.GONE);

							}

							@Override
							public void onLoadingComplete() {
								// TODO Auto-generated method stub
								holder.progress.setVisibility(View.GONE);
							}

							@Override
							public void onLoadingCancelled() {
								// TODO Auto-generated method stub

							}
						});
			else {
				holder.progress.setVisibility(View.GONE);
				imageLoader.displayImage("", holder.image, options);
			}
			TextView city = (TextView) item.findViewById(R.id.txt_city);
			TextView detail = (TextView) item.findViewById(R.id.detail);
			detail.setText(albumItem.getSong() + " - " + albumItem.getListen()
					+ " - " + albumItem.getVote());
			city.setText(albumItem.getTitle());
			return item;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}
	}

	@SuppressWarnings("unused")
	private class ViewHolder {
		public ImageView image;
		public TextView detail, city;
		public int position;
		public ProgressBar progress;
	}
}